<?php

namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\Request;
use think\response\Json;

class Feedback extends BaseController
{

    // 问题反馈列表
    public function list(Request $request): Json
    {
        try {
            //var_dump(111);die;
            $param = $request->param();
            $page = $param['page'] ?? 1;
            $limit = $param['limit'] ?? 10;
            $map = [];
            if ($param['mobile_phone'] ?? "") $map[] = ['mu.mobile_phone', 'like', '%' . $param['mobile_phone'] . '%'];
            if ($param['type'] ?? "") $map[] = ['fb.type', '=', $param['type']];
            if ($param['status'] ?? "") $map[] = ['fb.status', '=', $param['status']];

            $dataObj = Db::name('feedback')
                ->alias('fb')
                ->leftJoin('member_user mu', 'fb.sender_id = mu.id')
                ->field(["fb.*", "mu.nickname", "mu.mobile_phone", "(SELECT GROUP_CONCAT(image SEPARATOR '|') FROM member_feedback_image WHERE feedback_id = fb.feedback_id) as images"])
                ->where($map)
                ->fetchSql(false)
                ->order('fb.created_at', 'desc')
                ->page($page, $limit)
                ->select();
            //var_dump($dataObj);die();

            $dataArr = $dataObj->toArray();
            foreach ($dataArr as $key => $value) {
                $dataArr[$key]['sender_id'] = strval($value['sender_id']);
                $dataArr[$key]['feedback_id'] = strval($value['feedback_id']);
                //处理type和status

               // `type` tinyint(2) NOT NULL DEFAULT '1' COMMENT '问题类型1 => ''商品购买'', 2 => ''账号问题'', 3=> ''游戏闪退'', 4=> ''意见建议'', 5 => ''其他''',
                if ($value['type'] == 1) {
                    $dataArr[$key]['type_desc'] = '商品购买';
                } elseif ($value['type'] == 2) {
                    $dataArr[$key]['type_desc'] = '账号问题';
                } elseif ($value['type'] == 3) {
                    $dataArr[$key]['type_desc'] = '游戏闪退';
                } elseif ($value['type'] == 4) {
                    $dataArr[$key]['type_desc'] = '意见建议';
                } elseif ($value['type'] == 5) {
                    $dataArr[$key]['type_desc'] = '其他';
                } elseif ($value['type'] == 6) {
                    $dataArr[$key]['type_desc'] = '页面展示';
                }else{
                    $dataArr[$key]['type_desc'] = '未知类型';
                }
                //`status` tinyint(2) NOT NULL DEFAULT '1' COMMENT '问题状态1 => ''未读'', 2 => ''处理中'', 3 => ''已解决'', 4=> ''忽略''
                if ($value['status'] == 1) {
                    $dataArr[$key]['status_desc'] = '未读';
                } elseif ($value['status'] == 2) {
                    $dataArr[$key]['status_desc'] = '处理中';
                } elseif ($value['status'] == 3) {
                    $dataArr[$key]['status_desc'] = '已解决';
                } elseif ($value['status'] == 4) {
                    $dataArr[$key]['status_desc'] = '忽略';
                }else{
                    $dataArr[$key]['status_desc'] = '未知状态';
                }

                if ($value['is_reproducible'] == 0) {
                    $dataArr[$key]['is_reproducible_desc'] = '否';
                } else {
                    $dataArr[$key]['is_reproducible_desc'] = '是';
                }
                $dataArr[$key]['images'] = !empty($value['images']) ? explode('|', $value['images']) : [];
                foreach ($dataArr[$key]['images'] as $imageKey => $imageValue) {
                    $dataArr[$key]['images'][$imageKey] = config('basic.cdn') . $imageValue;
                }
            }
            $count = Db::name('feedback')->alias('fb')
                ->leftJoin('member_user mu', 'fb.sender_id = mu.id')
                ->fetchSql(false)
                ->where($map)
                ->count();
            return json(['code' => 0, 'msg' => 'success', 'data' => ['list' => $dataArr, 'count' => $count]]);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => '获取用户游戏库失败：' . $e->getMessage()]);
        }
    }

    // 修改问题描述和问题状态
    public function edit(Request $request): Json
    {
        try {
            $param = $request->param();
            $feedback = Db::name('feedback')->where('feedback_id', $param['feedback_id'] ?? '')->find();
            if (!$feedback) {
                return json(['code' => -1, 'msg' => '反馈不存在']);
            }
            if ($param['description'] ?? "") {
                $line['description'] = $param['description'];
            }
            if ($param['status'] ?? "") {
                $line['status'] = $param['status'];
            }
            $line['updated_at'] = date('Y-m-d H:i:s');
            Db::name('feedback')->where('feedback_id', $param['feedback_id'] ?? '')->fetchSql(false)->update($line);
            return json(['code' => 0, 'msg' => '修改成功']);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => '修改失败：' . $e->getMessage()]);
        }

    }


}